$(function(){
    $('#newNotice').modal({
    	show: false
    });
	$('.btn-invitation').click(function(){
		var uid= $(this).parents("tr").children("td").html();
    	$("#inviter").empty();
    	$("#invit-code").html("0");
		cAjax({  
	        data:"uid="+uid,
	        type:"get",  
	        url:"/cms/appUser/appUser/getUser", 
	        success:function(data){
	        	console.log(data);
	        	$("#invit-code").html(data.msg);
	        	$("#uid").val(uid);
	            $("#inviter").html(data.data.uname);
	            $("#msg").html(data.data.uname+"("+data.data.com_name+"-"+data.data.position+")"+"邀请您加入乔安"+data.data.head_url+"【乔安】");
	            $("#senderDesc").val(data.data.uname+"("+data.data.com_name+"-"+data.data.position+")");
	        }  
		});  
		$('#newNotice').modal('show');
	});
	
	$("#btn-send-invitation").click(function(){
		if($("#invit-code").html() == 0){
			alert("对不起，邀请码不足!");
			return false;
		}
		var data={};
		data['uid'] = $("input[name='uid']").val();
		data['senderDesc'] = $("input[name='senderDesc']").val();
		data['adminId'] = $("input[name='adminId']").val();
		data['adminMobile'] = $("input[name='adminMobile']").val();
		data['recName'] = $("input[name='recName']").val();
		data['recMobile'] = $("input[name='recMobile']").val();
		data['content'] =$("#msg").html();
		cAjax({  
	        data:data,
	        type:"post",
	        url:"/cms/appUser/appUser/sendInvitationCode", 
	        success:function(data){
	            $('#newNotice').modal('hide');
	            console.log(data.msg);
	            greeting({msg:"下发成功"});
	        }  
		});  
	});
	
	//搜索导丁
	$("#adminName").change();
	$("#adminName").keyup(function(e){
		if(event.keyCode == 13||event.keyCode == 8  ||event.keyCode == 40 ||event.keyCode == 32 ) {  
		  findP(this);
        }  
	});
	function findP(e){
		var $this = $(e);
		var seachWords = $this.val(); 
		var suggestTplStr = $('#app-guest .tpl').html();
		var $suggestContainer = $('.suggest');
		cAjax({
			url: '/cms/appUser/appUser/searchUserDing',
			type: 'POST',
			data: {
				keywords: seachWords
			},
			success: function(obj) {
				var items = obj['data'];
				$suggestContainer.empty();
				for(var index in items) {
					var data = items[index];
					var $itemEle = $(swig.render(suggestTplStr,{locals:{item:data}}));
					$itemEle.data('userData',data);
					$suggestContainer.append($itemEle);		
				}
			}
		});
	}
//	
//	$("#adminName").change(function(){
//		var $this = $(this);
//		var seachWords = $this.val(); 
//		var suggestTplStr = $('#app-guest .tpl').html();
//		var $suggestContainer = $('.suggest');
//		cAjax({
//			url: '/base/api/searchUserByName',
//			type: 'get',
//			data: {
//				keywords: seachWords
//			},
//			success: function(obj) {
//				var items = obj['data'];
//				$suggestContainer.empty();
//				for(var index in items) {
//					var data = items[index];
//					var $itemEle = $(swig.render(suggestTplStr,{locals:{item:data}}));
//					$itemEle.data('userData',data);
//					$suggestContainer.append($itemEle);		
//				}
//			}
//		});
//	})
	
	/*
	 * 添加用户
	 */
	$('.suggest').on('click','a',function(){
		var $this = $(this);
		var name = $this.find("span.name").html();
		$("#adminName").val(name);
		$("#adminId").val($this.find("span.user_id").html());
		$('.suggest').empty();
	});

});